﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace Ttifa.Infrastructure.Utils
{
    public class CommunicationHelper
    {
        public static string Http(string serviceUrl, string content)
        {
            HttpWebRequest request = WebRequest.Create(serviceUrl) as HttpWebRequest;
            request.Timeout = 60000;
            request.Method = "POST";
            request.ContentType = "text/xml; charset=GBK";
            byte[] data = Encoding.GetEncoding("GBK").GetBytes(content);
            request.ContentLength = data.Length;
            request.GetRequestStream().Write(data, 0, data.Length);

            WebResponse response = request.GetResponse();
            var resp = string.Empty;
            using (var stream = response.GetResponseStream())
            {
                var streamReader = new System.IO.StreamReader(stream, Encoding.GetEncoding("GBK"));
                resp = streamReader.ReadToEnd().Replace("\n", "");
            }

            return resp;
        }

        public static string Http(string serviceUrl, string contentType, string content, HttpMethod method, string encoding = "UTF-8")
        {
            HttpWebRequest request = WebRequest.Create(serviceUrl) as HttpWebRequest;
            request.Timeout = 60000;
            request.Method = method.ToString();
            request.ContentType = contentType;
            if (method == HttpMethod.POST || method == HttpMethod.PUT)
            {
                byte[] data = Encoding.GetEncoding(encoding).GetBytes(content);
                request.ContentLength = data.Length;
                request.GetRequestStream().Write(data, 0, data.Length);
            }

            WebResponse response = request.GetResponse();
            var resp = string.Empty;
            using (var stream = response.GetResponseStream())
            {
                var streamReader = new System.IO.StreamReader(stream, Encoding.GetEncoding(encoding));
                resp = streamReader.ReadToEnd().Replace("\n", "");
            }

            return resp;
        }

        public static string Tcp(string host, int port, string content)
        {
            TcpClient tcp = new TcpClient();
            tcp.Connect(host, port);//根据服务器的IP地址和侦听的端口连接

            var text = new StringBuilder();
            if (tcp.Connected)
            {
                //连接服务端的数据流
                NetworkStream streamToServer = tcp.GetStream();
                byte[] data = Encoding.GetEncoding("GBK").GetBytes(content);
                //send
                streamToServer.Write(data, 0, data.Length);     // 发往服务器
                text.Append($"send:{content}");

                //response
                data = new Byte[256];
                var size = streamToServer.Read(data, 0, data.Length);
                var responseData = Encoding.GetEncoding("GBK").GetString(data, 0, size);
                text.Append($";received:{responseData}");

                streamToServer.Close();
                tcp.Close();

            }

            return text.ToString();
        }
    }

    public enum HttpMethod
    {
        GET = 0,
        POST = 1,
        DELETE = 2,
        PUT = 3
    }
}
